package cricket

import (
	"longmen/server/config/global"
	cricket_behaviour2 "longmen/server/internal/services/nami/api/v5/sports/cricket/cricket_behaviour"
	"longmen/server/internal/services/nami/config"
	cricket2 "longmen/server/internal/services/nami/services/sports/cricket"
	"longmen/server/internal/services/nami/utils/restyclient"
)

type InitCricket struct {
}

func (f InitCricket) SyncTeamById() {
	id := 0
	for {
		resp := cricket_behaviour2.TeamApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.CRTeamsUrl+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取板球队伍失败: ", err)
			return
		}
		// 空数据
		if len(resp.Results) == 0 {
			global.Log.Info("板球队伍数据已更新完毕 ")
			break
		}
		if id == resp.Query.MaxID {
			global.Log.Info("板球队伍数据已刷新完成 ")
			return
		}
		last := resp.Results[len(resp.Results)-1]
		id = last.ID
		// 调用入库
		cricket2.CrateTeam(resp.Interface())
	}
}

func (f InitCricket) SyncUniqueTournamentById() {
	id := 0
	for {
		resp := cricket_behaviour2.UniqueTournamentApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.CRUniqueTournamentUrl+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取板球赛事失败: ", err)
			return
		}
		// 空数据
		if len(resp.Results) == 0 {
			global.Log.Info("板球赛事数据已更新完毕 ")
			return
		}
		if id == resp.Query.MaxID {
			global.Log.Info("板球赛事数据已刷新完成 ")
			return
		}
		last := resp.Results[len(resp.Results)-1]
		id = last.ID
		// 调用入库
		cricket2.CrateUniqueTournament(resp.Interface())
	}
}

// SyncVenue 获取赛事列表
// 返回全量赛事数据，可根据时间戳增量获取新增或变动的数据
//
// 1、首次全量更新，根据参数id获取全量数据 (在cricket_init.go 中进行全量手动初始化)
// 2、后续增量更新，根据参数time增量获取变动数据（建议请求频次：1min/次）
func (f InitCricket) SyncVenue() {
	var id int
	for {
		resp := cricket_behaviour2.VenueApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.CRVenue+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取板球场馆列表失败: ", err)
			return
		}

		// 调用入库
		cricket2.CrateVenue(resp.Results)
		id = resp.Query.MaxID
		if resp.Query.Total != 1000 {
			break
		}
	}
}

func (f InitCricket) SyncCompetitionById() {
	id := 0
	for {
		resp := cricket_behaviour2.UniqueTournamentApiResponse{}
		queryStr := config.BuildParamsAndUserAndSecret(map[string]interface{}{
			"id":    id,
			"limit": 1000,
		})
		err := restyclient.GetUrlAndBindRecords(config.CRUniqueTournamentUrl+queryStr, &resp)
		if err != nil {
			global.Log.Error("获取板球赛事失败: ", err)
			return
		}
		// 空数据
		if len(resp.Results) == 0 {
			global.Log.Info("板球赛事数据已更新完毕 ")
			return
		}
		if id == resp.Query.MaxID {
			global.Log.Info("板球赛事数据已刷新完成 ")
			return
		}
		last := resp.Results[len(resp.Results)-1]
		id = last.ID
		// 调用入库
		cricket2.CrateUniqueTournament(resp.Interface())
	}
}
